import sys
sys.path.append("../")
from model.CommonDb import *
from services.BaseService import *
from frameworks.utils.FileUtil import *
import pandas as pd
import datetime

class FeiShuService(BaseService):
    def __init__(self):
        super(FeiShuService, self).__init__()

    def setConf(self,conf):
        self.appid = conf[0]
        self.appsecret = conf[1]
        self.msgurl = 'https://open.feishu.cn/open-apis/im/v1/messages'
        self.getToken()

    def getToken(self):
        # 获取Access Token
        data = {
            'app_id': self.appid,
            'app_secret': self.appsecret
        }
        response = requests.post('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', json=data)
        self.access_token = response.json()['tenant_access_token']

    def sendMsg(self,msg):
        url = "https://open.feishu.cn/open-apis/im/v1/messages"
        params = {"receive_id_type": "user_id"}
        content = {
            "text": msg,
        }
        req = {
            "receive_id": "be686694",  # chat id
            "msg_type": "text",
            "content": json.dumps(content)
        }
        payload = json.dumps(req)
        headers = {
            'Authorization': f'Bearer {self.access_token}',
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", url, params=params, headers=headers, data=payload)
        print(response.headers['X-Tt-Logid'])  # for debug or oncall
        print(response.content)  # Print Response


